$(function() {
	validateRule();
});
$.validator.setDefaults({
	submitHandler : function() {
        if(isNull(colId)){
            layer.msg("请选择学院!",function () {
            });
        } else if(isNull(majId)){
            layer.msg("请选择专业!",function () {
            });
        } else if(isNull(claId)){
            layer.msg("请选择班级!",function () {
            });
        }else if(isNull(floorId)){
            layer.msg("请选择宿舍楼!",function () {
            });
        }else if(isNull(dormId)){
            layer.msg("请选择宿舍!",function () {
            });
        }else if(isNull(bedId)){
            layer.msg("请选择床铺!",function () {
            });
        }else {
            save();
        }
	}
});
function save() {
	var student = $('#studentForm').serialize();
	console.log(student);
	$.ajax({
		type : "post",
		url :  path.format("/dorm/student/addStudent"),
		data : student,

		async : false,
		error : function(request) {
		    console.log(request);
            layer.open({
                title: 'Warning!'
                ,content: 'Server Refused'
            });

        },
		success : function(r) {
		    var data = JSON.parse(r);
			if (data.status == 1000) {
				parent.layer.msg(data.desc,function () {
                    
                });
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引

				parent.layer.close(index);

			} else {
				parent.layer.msg(data.desc,function () {
                    
                });
			}
		}
	});
}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#studentForm").validate({
		rules : {
            stuName : {required : true},
            stuId : {required : true},
            stuPhone : {required : true},
            stuAge:{required:true,range:[18,100]},
            colId:{required:true},
            inDate:{required:true},
            stuEmail:{email:true}
		},
        messages : {
            stuName : {required : icon + "请输入学生姓名"},
            stuPhone : {required : icon + "请输入手机号"},
            stuId : {required : icon + "请输入学号"},
            stuAge:{required:icon + "请输入学生姓名",range:icon +"年龄必须在18-100之间"},
            inDate:{required:icon +"请输入入学日期"},
		}
	});
}